/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * mutationRateDialog.java
 *
 * Created on 07.01.2011, 17:21:37
 */

package gen_seq.ui;

import gen_seq.generator.Chromosome;
import gen_seq.generator.MutationOperator;
import gen_seq.generator.Population;
import javax.swing.JOptionPane;

/**
 *
 * @author akademi4eg
 */
public final class mutationRateDialog extends javax.swing.JDialog {

    /** Creates new form mutationRateDialog */
    public mutationRateDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        getRootPane().setDefaultButton(btnOk);
        fillValues();
    }

    public void fillValues ()
    {
        if (Population.getInstance().isRunning())
        {
            txtMutRate.setText(Double.toString(Math.round(Population.getInstance()
                                                         .getBestAchievement()
                                                         .getEvolutionOperator()
                                                         .getRate()*10000)/ 10000.0 ));
            txtRatePeriod.setText(Integer.toString(Chromosome.RATE_UPDATE_PERIOD));
            txtRateStep.setText(Double.toString(MutationOperator.RATE_STEP));
            chkKeepBest.setSelected(Population.keep_best);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnOk = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtMutRate = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtRatePeriod = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtRateStep = new javax.swing.JTextField();
        chkKeepBest = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mutation properties");
        setResizable(false);

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel1.setText("Mutation rate:");

        txtMutRate.setText("0.01");

        jLabel2.setText("Rate update period:");

        txtRatePeriod.setText("1");

        jLabel3.setText("Mutation rate step:");

        txtRateStep.setText("0.001");

        chkKeepBest.setText("Keep best chromosome");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMutRate, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtRateStep, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(txtRatePeriod, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(chkKeepBest)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(70, Short.MAX_VALUE)
                .addComponent(btnOk)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addGap(68, 68, 68))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMutRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtRatePeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtRateStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(chkKeepBest)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOk)
                    .addComponent(btnCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        double m_rate = 0;
        int m_period = 1;
        double m_step = 0;
        try {
            m_rate = Double.parseDouble(txtMutRate.getText());
            if (m_rate < 0.0 || m_rate > 1.0)
            {
                JOptionPane.showMessageDialog(this, "Mutation rate must be within [0,1] interval!", "Validation error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Mutation rate value is invalid.", "Validation error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            m_period = Integer.parseInt(txtRatePeriod.getText());
            if (m_period <= 0)
            {
                JOptionPane.showMessageDialog(this, "Mutation rate update period should be positive!", "Validation error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Mutation rate update period value is invalid.", "Validation error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            m_step = Double.parseDouble(txtRateStep.getText());
            if (m_step < 0.0 || m_step > 1.0)
            {
                JOptionPane.showMessageDialog(this, "Mutation rate step must be within [0,1] interval!", "Validation error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Mutation rate step value is invalid.", "Validation error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        boolean pop_activity = Population.getInstance().isActive();
        Population.getInstance().setActive(false);
        Chromosome.INITIAL_MUTATION_RATE = m_rate;
        if (Population.getInstance().isRunning())
        {
            Population.getInstance().getBestAchievement().getEvolutionOperator().setRate(m_rate);
            for (int i=0; i<Population.getInstance().getLength(); i++)
                Population.getInstance().getChromosome(i).getEvolutionOperator().setRate(m_rate);
        }
        Chromosome.RATE_UPDATE_PERIOD = m_period;
        MutationOperator.RATE_STEP = m_step;
        Population.keep_best = chkKeepBest.isSelected();
        Population.getInstance().setActive(pop_activity);
        dispose();
    }//GEN-LAST:event_btnOkActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JCheckBox chkKeepBest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField txtMutRate;
    private javax.swing.JTextField txtRatePeriod;
    private javax.swing.JTextField txtRateStep;
    // End of variables declaration//GEN-END:variables

}
